{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1 获取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "datapath=\"D:\\\\2020\\\\Python\\\\MachineLearning(Study)\\\\data\\\\facebook-v-predicting-check-ins\\\\\"\n",
    "data=pd.read_csv(datapath + \"train.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.7941</td>\n",
       "      <td>9.0809</td>\n",
       "      <td>54</td>\n",
       "      <td>470702</td>\n",
       "      <td>8523065625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>5.9567</td>\n",
       "      <td>4.7968</td>\n",
       "      <td>13</td>\n",
       "      <td>186555</td>\n",
       "      <td>1757726713</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>8.3078</td>\n",
       "      <td>7.0407</td>\n",
       "      <td>74</td>\n",
       "      <td>322648</td>\n",
       "      <td>1137537235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>7.3665</td>\n",
       "      <td>2.5165</td>\n",
       "      <td>65</td>\n",
       "      <td>704587</td>\n",
       "      <td>6567393236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>4.0961</td>\n",
       "      <td>1.1307</td>\n",
       "      <td>31</td>\n",
       "      <td>472130</td>\n",
       "      <td>7440663949</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29118016</th>\n",
       "      <td>29118016</td>\n",
       "      <td>6.5133</td>\n",
       "      <td>1.1435</td>\n",
       "      <td>67</td>\n",
       "      <td>399740</td>\n",
       "      <td>8671361106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29118017</th>\n",
       "      <td>29118017</td>\n",
       "      <td>5.9186</td>\n",
       "      <td>4.4134</td>\n",
       "      <td>67</td>\n",
       "      <td>125480</td>\n",
       "      <td>9077887898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29118018</th>\n",
       "      <td>29118018</td>\n",
       "      <td>2.9993</td>\n",
       "      <td>6.3680</td>\n",
       "      <td>67</td>\n",
       "      <td>737758</td>\n",
       "      <td>2838334300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29118019</th>\n",
       "      <td>29118019</td>\n",
       "      <td>4.0637</td>\n",
       "      <td>8.0061</td>\n",
       "      <td>70</td>\n",
       "      <td>764975</td>\n",
       "      <td>1007355847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29118020</th>\n",
       "      <td>29118020</td>\n",
       "      <td>7.4523</td>\n",
       "      <td>2.0871</td>\n",
       "      <td>17</td>\n",
       "      <td>102842</td>\n",
       "      <td>7028698129</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>29118021 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            row_id       x       y  accuracy    time    place_id\n",
       "0                0  0.7941  9.0809        54  470702  8523065625\n",
       "1                1  5.9567  4.7968        13  186555  1757726713\n",
       "2                2  8.3078  7.0407        74  322648  1137537235\n",
       "3                3  7.3665  2.5165        65  704587  6567393236\n",
       "4                4  4.0961  1.1307        31  472130  7440663949\n",
       "...            ...     ...     ...       ...     ...         ...\n",
       "29118016  29118016  6.5133  1.1435        67  399740  8671361106\n",
       "29118017  29118017  5.9186  4.4134        67  125480  9077887898\n",
       "29118018  29118018  2.9993  6.3680        67  737758  2838334300\n",
       "29118019  29118019  4.0637  8.0061        70  764975  1007355847\n",
       "29118020  29118020  7.4523  2.0871        17  102842  7028698129\n",
       "\n",
       "[29118021 rows x 6 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2 数据处理\n",
    "确定特征值和目标值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.7941</td>\n",
       "      <td>9.0809</td>\n",
       "      <td>54</td>\n",
       "      <td>470702</td>\n",
       "      <td>8523065625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>5.9567</td>\n",
       "      <td>4.7968</td>\n",
       "      <td>13</td>\n",
       "      <td>186555</td>\n",
       "      <td>1757726713</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>8.3078</td>\n",
       "      <td>7.0407</td>\n",
       "      <td>74</td>\n",
       "      <td>322648</td>\n",
       "      <td>1137537235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>7.3665</td>\n",
       "      <td>2.5165</td>\n",
       "      <td>65</td>\n",
       "      <td>704587</td>\n",
       "      <td>6567393236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>4.0961</td>\n",
       "      <td>1.1307</td>\n",
       "      <td>31</td>\n",
       "      <td>472130</td>\n",
       "      <td>7440663949</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   row_id       x       y  accuracy    time    place_id\n",
       "0       0  0.7941  9.0809        54  470702  8523065625\n",
       "1       1  5.9567  4.7968        13  186555  1757726713\n",
       "2       2  8.3078  7.0407        74  322648  1137537235\n",
       "3       3  7.3665  2.5165        65  704587  6567393236\n",
       "4       4  4.0961  1.1307        31  472130  7440663949"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>112</th>\n",
       "      <td>112</td>\n",
       "      <td>2.2360</td>\n",
       "      <td>1.3655</td>\n",
       "      <td>66</td>\n",
       "      <td>623174</td>\n",
       "      <td>7663031065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>180</th>\n",
       "      <td>180</td>\n",
       "      <td>2.2003</td>\n",
       "      <td>1.2541</td>\n",
       "      <td>65</td>\n",
       "      <td>610195</td>\n",
       "      <td>2358558474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>367</th>\n",
       "      <td>367</td>\n",
       "      <td>2.4108</td>\n",
       "      <td>1.3213</td>\n",
       "      <td>74</td>\n",
       "      <td>579667</td>\n",
       "      <td>6644108708</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>874</th>\n",
       "      <td>874</td>\n",
       "      <td>2.0822</td>\n",
       "      <td>1.1973</td>\n",
       "      <td>320</td>\n",
       "      <td>143566</td>\n",
       "      <td>3229876087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1022</th>\n",
       "      <td>1022</td>\n",
       "      <td>2.0160</td>\n",
       "      <td>1.1659</td>\n",
       "      <td>65</td>\n",
       "      <td>207993</td>\n",
       "      <td>3244363975</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29115112</th>\n",
       "      <td>29115112</td>\n",
       "      <td>2.1889</td>\n",
       "      <td>1.2914</td>\n",
       "      <td>168</td>\n",
       "      <td>721885</td>\n",
       "      <td>4606837364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29115204</th>\n",
       "      <td>29115204</td>\n",
       "      <td>2.1193</td>\n",
       "      <td>1.4692</td>\n",
       "      <td>58</td>\n",
       "      <td>563389</td>\n",
       "      <td>2074133146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29115338</th>\n",
       "      <td>29115338</td>\n",
       "      <td>2.0007</td>\n",
       "      <td>1.4852</td>\n",
       "      <td>25</td>\n",
       "      <td>765986</td>\n",
       "      <td>6691588909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29115464</th>\n",
       "      <td>29115464</td>\n",
       "      <td>2.4132</td>\n",
       "      <td>1.4237</td>\n",
       "      <td>61</td>\n",
       "      <td>151918</td>\n",
       "      <td>7396159924</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29117493</th>\n",
       "      <td>29117493</td>\n",
       "      <td>2.2948</td>\n",
       "      <td>1.0504</td>\n",
       "      <td>81</td>\n",
       "      <td>79569</td>\n",
       "      <td>1168869217</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>83197 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            row_id       x       y  accuracy    time    place_id\n",
       "112            112  2.2360  1.3655        66  623174  7663031065\n",
       "180            180  2.2003  1.2541        65  610195  2358558474\n",
       "367            367  2.4108  1.3213        74  579667  6644108708\n",
       "874            874  2.0822  1.1973       320  143566  3229876087\n",
       "1022          1022  2.0160  1.1659        65  207993  3244363975\n",
       "...            ...     ...     ...       ...     ...         ...\n",
       "29115112  29115112  2.1889  1.2914       168  721885  4606837364\n",
       "29115204  29115204  2.1193  1.4692        58  563389  2074133146\n",
       "29115338  29115338  2.0007  1.4852        25  765986  6691588909\n",
       "29115464  29115464  2.4132  1.4237        61  151918  7396159924\n",
       "29117493  29117493  2.2948  1.0504        81   79569  1168869217\n",
       "\n",
       "[83197 rows x 6 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这里为了快速实现，缩小数据范围\n",
    "#2<x<2.5\n",
    "#1.0<y<1.5\n",
    "\n",
    "data=data.query(\"x>2 & x<2.5 & y>1 & y<1.5\")\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\pycharm_project\\venv\\machinelearning\\lib\\site-packages\\ipykernel_launcher.py:5: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  \"\"\"\n",
      "d:\\pycharm_project\\venv\\machinelearning\\lib\\site-packages\\ipykernel_launcher.py:6: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  \n",
      "d:\\pycharm_project\\venv\\machinelearning\\lib\\site-packages\\ipykernel_launcher.py:7: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  import sys\n"
     ]
    }
   ],
   "source": [
    "# 将时间戳处理为年月日\n",
    "timevalue=pd.to_datetime(data['time'],unit='s')\n",
    "type(timevalue)\n",
    "date=pd.DatetimeIndex(timevalue)\n",
    "data['day']=date.day\n",
    "data['weekday']=date.weekday\n",
    "data['hour']=date.hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "      <th>hour</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>112</th>\n",
       "      <td>112</td>\n",
       "      <td>2.2360</td>\n",
       "      <td>1.3655</td>\n",
       "      <td>66</td>\n",
       "      <td>623174</td>\n",
       "      <td>7663031065</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>367</th>\n",
       "      <td>367</td>\n",
       "      <td>2.4108</td>\n",
       "      <td>1.3213</td>\n",
       "      <td>74</td>\n",
       "      <td>579667</td>\n",
       "      <td>6644108708</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>874</th>\n",
       "      <td>874</td>\n",
       "      <td>2.0822</td>\n",
       "      <td>1.1973</td>\n",
       "      <td>320</td>\n",
       "      <td>143566</td>\n",
       "      <td>3229876087</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1022</th>\n",
       "      <td>1022</td>\n",
       "      <td>2.0160</td>\n",
       "      <td>1.1659</td>\n",
       "      <td>65</td>\n",
       "      <td>207993</td>\n",
       "      <td>3244363975</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1045</th>\n",
       "      <td>1045</td>\n",
       "      <td>2.3859</td>\n",
       "      <td>1.1660</td>\n",
       "      <td>498</td>\n",
       "      <td>503378</td>\n",
       "      <td>6438240873</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29115112</th>\n",
       "      <td>29115112</td>\n",
       "      <td>2.1889</td>\n",
       "      <td>1.2914</td>\n",
       "      <td>168</td>\n",
       "      <td>721885</td>\n",
       "      <td>4606837364</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29115204</th>\n",
       "      <td>29115204</td>\n",
       "      <td>2.1193</td>\n",
       "      <td>1.4692</td>\n",
       "      <td>58</td>\n",
       "      <td>563389</td>\n",
       "      <td>2074133146</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29115338</th>\n",
       "      <td>29115338</td>\n",
       "      <td>2.0007</td>\n",
       "      <td>1.4852</td>\n",
       "      <td>25</td>\n",
       "      <td>765986</td>\n",
       "      <td>6691588909</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29115464</th>\n",
       "      <td>29115464</td>\n",
       "      <td>2.4132</td>\n",
       "      <td>1.4237</td>\n",
       "      <td>61</td>\n",
       "      <td>151918</td>\n",
       "      <td>7396159924</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29117493</th>\n",
       "      <td>29117493</td>\n",
       "      <td>2.2948</td>\n",
       "      <td>1.0504</td>\n",
       "      <td>81</td>\n",
       "      <td>79569</td>\n",
       "      <td>1168869217</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>80910 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            row_id       x       y  accuracy    time    place_id  day  \\\n",
       "112            112  2.2360  1.3655        66  623174  7663031065    8   \n",
       "367            367  2.4108  1.3213        74  579667  6644108708    7   \n",
       "874            874  2.0822  1.1973       320  143566  3229876087    2   \n",
       "1022          1022  2.0160  1.1659        65  207993  3244363975    3   \n",
       "1045          1045  2.3859  1.1660       498  503378  6438240873    6   \n",
       "...            ...     ...     ...       ...     ...         ...  ...   \n",
       "29115112  29115112  2.1889  1.2914       168  721885  4606837364    9   \n",
       "29115204  29115204  2.1193  1.4692        58  563389  2074133146    7   \n",
       "29115338  29115338  2.0007  1.4852        25  765986  6691588909    9   \n",
       "29115464  29115464  2.4132  1.4237        61  151918  7396159924    2   \n",
       "29117493  29117493  2.2948  1.0504        81   79569  1168869217    1   \n",
       "\n",
       "          weekday  hour  \n",
       "112             3     5  \n",
       "367             2    17  \n",
       "874             4    15  \n",
       "1022            5     9  \n",
       "1045            1    19  \n",
       "...           ...   ...  \n",
       "29115112        4     8  \n",
       "29115204        2    12  \n",
       "29115338        4    20  \n",
       "29115464        4    18  \n",
       "29117493        3    22  \n",
       "\n",
       "[80910 rows x 9 columns]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 过滤掉签到次数少的地点\n",
    "placecount=data.groupby(\"place_id\").count()['row_id']\n",
    "datafinal=data[data['place_id'].isin(placecount[placecount > 3 ].index.values)]\n",
    "datafinal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "      <th>hour</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>112</th>\n",
       "      <td>2.2360</td>\n",
       "      <td>1.3655</td>\n",
       "      <td>66</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>367</th>\n",
       "      <td>2.4108</td>\n",
       "      <td>1.3213</td>\n",
       "      <td>74</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>874</th>\n",
       "      <td>2.0822</td>\n",
       "      <td>1.1973</td>\n",
       "      <td>320</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1022</th>\n",
       "      <td>2.0160</td>\n",
       "      <td>1.1659</td>\n",
       "      <td>65</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1045</th>\n",
       "      <td>2.3859</td>\n",
       "      <td>1.1660</td>\n",
       "      <td>498</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29115112</th>\n",
       "      <td>2.1889</td>\n",
       "      <td>1.2914</td>\n",
       "      <td>168</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29115204</th>\n",
       "      <td>2.1193</td>\n",
       "      <td>1.4692</td>\n",
       "      <td>58</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29115338</th>\n",
       "      <td>2.0007</td>\n",
       "      <td>1.4852</td>\n",
       "      <td>25</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29115464</th>\n",
       "      <td>2.4132</td>\n",
       "      <td>1.4237</td>\n",
       "      <td>61</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29117493</th>\n",
       "      <td>2.2948</td>\n",
       "      <td>1.0504</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>80910 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               x       y  accuracy  day  weekday  hour\n",
       "112       2.2360  1.3655        66    8        3     5\n",
       "367       2.4108  1.3213        74    7        2    17\n",
       "874       2.0822  1.1973       320    2        4    15\n",
       "1022      2.0160  1.1659        65    3        5     9\n",
       "1045      2.3859  1.1660       498    6        1    19\n",
       "...          ...     ...       ...  ...      ...   ...\n",
       "29115112  2.1889  1.2914       168    9        4     8\n",
       "29115204  2.1193  1.4692        58    7        2    12\n",
       "29115338  2.0007  1.4852        25    9        4    20\n",
       "29115464  2.4132  1.4237        61    2        4    18\n",
       "29117493  2.2948  1.0504        81    1        3    22\n",
       "\n",
       "[80910 rows x 6 columns]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 筛选特征值和目标值\n",
    "x = datafinal[['x','y','accuracy','day','weekday','hour']]\n",
    "y = datafinal['place_id']\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据集划分\n",
    "from sklearn.model_selection import train_test_split\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3 特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.model_selection import GridSearchCV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\pycharm_project\\venv\\machinelearning\\lib\\site-packages\\sklearn\\model_selection\\_split.py:672: UserWarning: The least populated class in y has only 1 members, which is less than n_splits=10.\n",
      "  % (min_groups, self.n_splits)), UserWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "798474       True\n",
      "12148464    False\n",
      "2269451      True\n",
      "11462032     True\n",
      "26472375     True\n",
      "            ...  \n",
      "25634495    False\n",
      "9819159     False\n",
      "4353090     False\n",
      "9390771     False\n",
      "3264316     False\n",
      "Name: place_id, Length: 20228, dtype: bool\n",
      "0.36147913782875224\n",
      "{'n_neighbors': 7}\n",
      "0.3601891421580847\n",
      "KNeighborsClassifier(n_neighbors=7)\n",
      "{'mean_fit_time': array([0.11563671, 0.12154443, 0.12257624, 0.118257  ]), 'std_fit_time': array([0.00553508, 0.00373776, 0.00406512, 0.00163717]), 'mean_score_time': array([0.23404999, 0.26572468, 0.28130889, 0.28579161]), 'std_score_time': array([0.01760264, 0.01034882, 0.0080492 , 0.0022183 ]), 'param_n_neighbors': masked_array(data=[3, 5, 7, 9],\n",
      "             mask=[False, False, False, False],\n",
      "       fill_value='?',\n",
      "            dtype=object), 'params': [{'n_neighbors': 3}, {'n_neighbors': 5}, {'n_neighbors': 7}, {'n_neighbors': 9}], 'split0_test_score': array([0.35656616, 0.37337288, 0.3720547 , 0.36431043]), 'split1_test_score': array([0.33481628, 0.34700939, 0.35079914, 0.34486736]), 'split2_test_score': array([0.34723138, 0.36354647, 0.36387607, 0.35646012]), 'split3_test_score': array([0.35563612, 0.35365854, 0.35810811, 0.35085695]), 'split4_test_score': array([0.33998022, 0.35695452, 0.3587673 , 0.35596572]), 'split5_test_score': array([0.34805537, 0.36552406, 0.36766645, 0.36272248]), 'split6_test_score': array([0.34212261, 0.35349374, 0.35431773, 0.35250494]), 'split7_test_score': array([0.34475939, 0.35843771, 0.36107449, 0.35266974]), 'split8_test_score': array([0.35168095, 0.37261042, 0.36354647, 0.35827291]), 'split9_test_score': array([0.33684904, 0.34805537, 0.35168095, 0.34739618]), 'mean_test_score': array([0.34576975, 0.35926631, 0.36018914, 0.35460268]), 'std_test_score': array([0.00709654, 0.00883888, 0.00650951, 0.00589769]), 'rank_test_score': array([4, 2, 1, 3])}\n"
     ]
    }
   ],
   "source": [
    "transfer=StandardScaler()\n",
    "x_train=transfer.fit_transform(x_train)\n",
    "x_test = transfer.transform(x_test)\n",
    "estimator=KNeighborsClassifier(n_neighbors=3)\n",
    "\n",
    "#网格搜索与交叉验证\n",
    "estimator=GridSearchCV(estimator=estimator,\n",
    "             param_grid={'n_neighbors':[3,5,7,9]},cv=10)\n",
    "estimator.fit(x_train,y_train)\n",
    "\n",
    "y_predict=estimator.predict(x_test)\n",
    "print(y_test==y_predict)\n",
    "score=estimator.score(x_test,y_test)\n",
    "print(score)\n",
    "\n",
    "print(estimator.best_params_)\n",
    "print(estimator.best_score_)\n",
    "print(estimator.best_estimator_)\n",
    "print(estimator.cv_results_)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
